#include<bits/stdc++.h>
#define ll long long
#define max(n, m) ((n) > (m) ? (n) : (m))
using namespace std;
int n, T;
int a[2005];
ll dp[2005][2005], maxn[2005];

int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    cin >> T;
    while(T--)
    {
        memset(a, 0, sizeof a);
        memset(dp, 0, sizeof dp);
        memset(maxn, 0, sizeof maxn);
        cin >> n;
        for(int i = 1; i <= n; i++)
            cin >> a[i];
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j < i; j++)
            {
                if(j == i - 1)
                {
                    for(int k = 0; k < i - 1; k++)
                    {
                        if(dp[j][k] + (a[i] == a[k] ? a[i] : 0) > dp[i][j])
                        {
                            dp[i][j] = dp[j][k] + (a[i] == a[k] ? a[i] : 0);
//                            cout << endl << i << " " << j << " " << k << endl;
                        }
                    }
                }
                else
                    dp[i][j] = dp[i - 1][j] + (a[i] == a[i - 1] ? a[i] : 0);
                maxn[i] = max(maxn[i], dp[i][j]);
//                cout << dp[i][j] << " ";
            }
//            cout << endl;
        }
        cout << maxn[n] << endl;
    }
    return 0;
}
